from django.shortcuts import render
from django.http.response import JsonResponse
from django.http import HttpResponse,HttpResponseNotFound
#引入用户模型
from reptile.models import Users
#引入图片模型
from reptile.models import IMG
#跨域请求
from django.views.decorators.csrf import csrf_exempt
import random
import json

# Create your views here.

def index(request):
    # return HttpResponse("登录页面")
    return render(request,"Login.html")

#随机账户注册
def addUser(request):
    try:
        u = Users()
        username = ""
        password = ""
        for i in range(6):
            username +=repr(random.randint(0, 9))
        for i in range(6):
            password +=repr(random.randint(0,9))
        u.username=username
        u.password=password
        u.save()
        return HttpResponse("用户：%s 密码：%s 注册成功"%(username,password))
    except Exception as e:
        return HttpResponse("注册用户失败")


def delUser(request,username):
    try:
        u = Users.objects
        u = u.get(username=username)
        u.delete()
        return HttpResponse("删除成功账户:%s"%(username))
    except Exception as e:
        return HttpResponse("删除用户失败")

def upUser(request,username,password):
    try:
        u = Users.objects
        u = u.get(username=username)
        u.password = password
        u.save()
        return HttpResponse("更新账户：%s 密码为:%s 成功"%(username,password))
    except Exception as e:
        return HttpResponse("更新用户失败")

def selectUser(request,username):
    print(username)
    try:
        u = Users.objects
        u = u.get(username = username)
        id = u.id
        return HttpResponse("用户的id为：%s"%(id))
    except Exception as e:
        return HttpResponse("查询用户失败")

def allUser(request):
    try:
        u = Users.objects
        u = u.all()
        list =[]
        for i in u:
            dict = {}
            dict['id'] = i.id
            dict['username']=i.username
            dict['password'] = i.password
            list.append(dict)
        return HttpResponse(json.dumps(list),content_type='application/json')
        #dict.update(dict)
    except Exception as e:
        return HttpResponse("查询所有用户失败")

#登录功能
def loginUser(request,username,pwd):
    try:
        u = Users.objects
        u = u.get( username = username ,password = pwd)
        return HttpResponse("登录账户成功")
    except Exception as e:
        return HttpResponse("账户或密码错误")

#测试 book是否连通
def books(request):
    books =[
        {"id":1,"title":"python","price":"88.0"},
        {"id":2,"title":"flask","price":"88.0"},
        {"id":3,"title":"django","price":"88.0"}
    ]
    return JsonResponse(books,safe = False)

#上传图片功能
@csrf_exempt
def uploadImg(request):
    if request.method == 'POST':
        FileList =request.FILES.getlist('img')
        for i in FileList:
            new_img =IMG(
                img = i,
                name = i.name
            )
            new_img.save()
    return render(request, 'img_tem/uploadimg.html')


#查看图片功能
@csrf_exempt
def showImg(request):
    imgsList=[]
    imgs = IMG.objects.all()
    # 返回对象
    # content = {
    #     'imgs':imgs,
    # }
    for i in imgs:
        imgsList.append("."+i.img.url)
    # return render(request, 'img_tem/showimg.html', content)
    return JsonResponse(imgsList,safe=False)

def show(request):
    return render(request, 'img_tem/showimg.html')